package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysStudentScoreDao;
import com.groupfifteen.entity.SysStudentScoreEntity;
import com.groupfifteen.service.SysStudentScoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysStudentScoreService")
@RequiredArgsConstructor
public class SysStudentScoreServiceImpl extends ServiceImpl<SysStudentScoreDao, SysStudentScoreEntity> implements SysStudentScoreService {
    private final SysStudentScoreDao scoreMapper;
    @Override
    public IPage<SysStudentScoreEntity> QueryStudentScores(SysStudentScoreEntity sysStudentScoreEntity, int pageNo, int pageSize) {
        // 创建Lambda查询条件
        LambdaQueryWrapper<SysStudentScoreEntity> query = Wrappers.lambdaQuery();
        // 假设我们根据examCode进行查询，您可以根据实际需求添加更多的查询条件
        query.eq(sysStudentScoreEntity.getExamCode() != null, SysStudentScoreEntity::getExamCode, sysStudentScoreEntity.getExamCode());

        // 确保页码和页大小有效
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }

        // 创建分页对象
        IPage<SysStudentScoreEntity> page = new Page<>(pageNo, pageSize);
        // 执行分页查询
        return baseMapper.selectPage(page, query);
    }

    // 在这里实现 SysStudentScoreService 接口中的其他方法

    @Override
    public Integer AddStudentScore(SysStudentScoreEntity sysStudentScoreEntity) {
        return baseMapper.insert(sysStudentScoreEntity);
    }

    @Override
    public Integer DeleteStudentScore(Integer ScoreId) {
        return baseMapper.deleteById(ScoreId);
    }

    @Override
    public SysStudentScoreEntity GetStudentScore(Integer ScoreId) {
        return baseMapper.selectById(ScoreId);
    }

    @Override
    public Integer UpdateStudentScore(SysStudentScoreEntity sysStudentScoreEntity) {
        return baseMapper.updateById(sysStudentScoreEntity);
    }
    /**
     * 添加学生成绩
     *
     * @param score 学生成绩
     * @return 考试成绩
     */
    @Override
    public Integer add(SysStudentScoreEntity score) {
        return scoreMapper.add(score);
    }

    /**
     * 查询所有考试成绩
     *
     * @return 考试成绩
     */
    @Override
    public List<SysStudentScoreEntity> findAll() {
        return scoreMapper.findAll();
    }

    /**
     * 查询考试成绩 分页
     *
     * @param page 分页对象
     * @param studentId 学生id
     * @return 考试成绩
     */
    @Override
    public IPage<SysStudentScoreEntity> findById(Page page, Integer studentId) {
        return scoreMapper.findById(page, studentId);
    }

    /**
     * 查询考试成绩 不分页
     *
     * @param studentId 学生id
     * @return 考试成绩
     */
    @Override
    public List<SysStudentScoreEntity> findById(Integer studentId) {
        Page<SysStudentScoreEntity> scorePage = new Page<>(0, 9999);
        return scoreMapper.findById(scorePage, studentId).getRecords();
    }

    /**
     * 通过考试编号 查询学生成绩
     *
     * @param examCode 考试编号
     * @return 考试成绩
     */
    @Override
    public List<SysStudentScoreEntity> findByExamCode(Integer examCode) {
        return scoreMapper.findByExamCode(examCode);
    }
}